﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Threading;
using System.Threading;

namespace Hugo.UIPages
{
    public class SkypeOutgoingCallUIPage : UIPage
    {
        private string displayName;
        private System.Windows.Shapes.Rectangle accept;
        private int indexOfSelectedImage;
        public static System.Media.SoundPlayer sound;
        private  DispatcherTimer readyTimer;

        public SkypeOutgoingCallUIPage(String displayName)
            : base("Outgoing call", Util.getBitmapSource(Properties.Resources.skype))
        {
            Console.WriteLine("Looking up contact " + displayName);
            //com.skype.api.Contact c = Communicate.getInstance().getContactWithDisplayName(displayName);

            this.displayName = displayName;
            
            Label instructionsLabel = new Label();
            instructionsLabel.Content = Translate.translate("Calling ") + displayName.Trim();
            instructionsLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            instructionsLabel.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
            instructionsLabel.FontSize = GlobalSettings.SelectedLabelFontSize;
            instructionsLabel.Margin = new System.Windows.Thickness(0, GlobalSettings.EffectiveScreenMiddleY-250, 0, 0);

            //Rectangle rcaller = Util.getSymbol(Util.getImageForContact(c));

            Grid g = new Grid();
            g.Width = GlobalSettings.ScreenMiddleX * 2;
            PageCanvas.Children.Add(g);

            g.Children.Add(instructionsLabel);

            accept = Util.getSymbol(Util.getBitmapSource(Properties.Resources.callend));

            //PageCanvas.Children.Add(rcaller);
            PageCanvas.Children.Add(accept);

            
            //Canvas.SetTop(rcaller, GlobalSettings.EffectiveScreenMiddleY);
            //Canvas.SetLeft(rcaller, GlobalSettings.ScreenMiddleX - rcaller.Width/2);
            
            Canvas.SetTop(accept, GlobalSettings.EffectiveScreenMiddleY - 100);
            Canvas.SetLeft(accept, GlobalSettings.ScreenMiddleX - accept.Width/2);

            this.isBackButtonVisible = false;
        }

        public override bool respondToClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (indexOfSelectedImage != -1)
            {
                // cancel
                if (indexOfSelectedImage == 0)
                {
                    if (Communicate.activeParticipant != null) { 
                        Communicate.activeParticipant.hangup();
                        Console.WriteLine("C hungup");
                    }
                    MainWindow.popPage();
                }

                sound.Stop();

                return true;
            }

            return false;
        }

        public override void setPage()
        {
            restartSleepTimer();
            sound = Sounds.playSound(Sounds.Sound.Call, true);
            base.setPage();

            Thread Messagethread = new Thread(
                    new ThreadStart(delegate()
                    {
                        MainWindow.getInstance().Dispatcher.Invoke(
                          System.Windows.Threading.DispatcherPriority.Normal,
                          new Action(
                            delegate()
                            {
                                Communicate.getInstance().doMakeCall(this.displayName);
                            }
                        ));
                    }));
            Messagethread.SetApartmentState(ApartmentState.STA);
            Messagethread.Start();

            
            
        }

        public override void  unSetPage()
        {
            if (readyTimer != null)
            {
                readyTimer.Stop();
            }
 	         base.unSetPage();
        }

        private void restartSleepTimer()
        {
            if (readyTimer != null)
            {
                readyTimer.Stop();
                readyTimer.Start();
            }
            else
            {
                readyTimer = new DispatcherTimer();
                readyTimer.Interval = TimeSpan.FromMilliseconds(000);
                readyTimer.Tick += new EventHandler(readyTimer_Tick);
                readyTimer.Start();
            }
        }

        void readyTimer_Tick(object sender, EventArgs e)
        {
            //Console.WriteLine("Timer " + Communicate.mySession.callActive);
            // call inactive, stop trying
            if (!Communicate.mySession.callActive)
            {
                Console.WriteLine("FORCIBLY HANGING UP");
                sound.Stop();
                readyTimer.Stop();
                readyTimer = null;
                MainWindow.popPage();
            }

                // call active, restart timer
            else
            {
                restartSleepTimer();
            }
            
        }

        public override bool setCursorPosition(System.Windows.Point p)
        {
            int ind = getIndexOfImageAtPosition(p);

            if (ind != -1)
            {
                return true;
            }

            return false;
        }

        private int getIndexOfImageAtPosition(System.Windows.Point p)
        {
            int index = -1;

            int i = 0;
            foreach (Rectangle o in new List<Rectangle> { accept })
            {
                Rectangle im = o;

                Point pos = im.PointToScreen(new Point());

                double rX = pos.X - HandCursor.handSize / 2;
                double rY = pos.Y - HandCursor.handSize / 2;
                double rX2 = rX + im.ActualWidth;
                double rY2 = rY + im.ActualHeight;

                if (rX < p.X && rY < p.Y && rX2 > p.X && rY2 > p.Y)
                {
                    index = i;
                    break;
                }
                i++;
            }

            indexOfSelectedImage = index;

            return index;
        }


        

    }
}
